package com.cai.commons.interceptors;

import com.cai.bean.User;
import com.cai.commons.utils.StaticCode;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author CWX
 * @Date 2017/7/22 20:21
 * 拦截器
 */
public class MyInterceptors implements HandlerInterceptor {
        /**
         * @Author CWX
         * @Date 2017/7/26 15:54
         * url
         */
    public static boolean equalsUrl(String url) {
        List<String> urlList=new ArrayList<>();
        urlList.add("/login");
        urlList.add("/login.html");
        urlList.add("/index");
        urlList.add("/jsFile");
        Iterator<String> list=urlList.iterator();
        while (list.hasNext()){
            String result=list.next();
            if (result.equals(url)){//相等
                return true;
            }
        }
        return false;
    }
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        String url=httpServletRequest.getRequestURI();
        HttpSession httpSession=httpServletRequest.getSession();
        User user= (User) httpSession.getAttribute(StaticCode.LOGINSESSION);
        System.out.println(url);
        boolean result=equalsUrl(url);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}
